package com.ruoyi.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.common.Result;
import com.ruoyi.business.entity.Features;
import com.ruoyi.business.service.FrontFeaturesService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ao
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/features")
public class FrontFeaturesController {

    @Autowired
    private FrontFeaturesService featuresService;

    //获取所有特色信息
    @ApiOperation(value = "获取所有特色信息", tags = "获取所有特色信息")
    @GetMapping("/list")
    public Result<List<Features>> getList() {
        List<Features> list = featuresService.list();

        for(Features feature:list){

            String[] split = feature.getPictures().split(",");
            feature.setPictureList(Arrays.asList(split));

        }
        return Result.success(list);

    }

    //获取指定特定民宿下的所有特色
    @ApiOperation(value = "获取特色民宿信息", tags = "获取所有特色信息")
    @GetMapping("/getinfo")
    public Result<List<Features>> getFeaturesList(@Param("homestayid") Long homestayid) {
        if (homestayid == null) {
            return Result.error("参数获取失败！");
        }
        LambdaQueryWrapper<Features> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Features::getHomestayId,homestayid);
        List<Features> list = featuresService.list(queryWrapper);
        for(Features feature:list){
            String[] split = feature.getPictures().split(",");
            feature.setPictureList(Arrays.asList(split));
        }
        return Result.success(list);

    }

    //获取特色详情
    @ApiOperation(value = "获取特色详情",tags = "获取特色详情")
    @GetMapping("/getdetail")
    public Result<Features> getDetail(@Param("featureid") Long featureid){
        if(featureid==null){
            return Result.error("参数获取失败");
        }
        Features byId = featuresService.getById(featureid);
        if(byId==null){
            return Result.error("数据不存在");
        }
        return Result.success(byId);
    }



}
